import com.troyworks.framework.tags.TagIndex;
import com.troyworks.util.BitFlag;
import com.troyworks.framework.tags.TagSpace;
import com.troyworks.util.MaskedBitFlag;
/**
 * @author Troy Gardner
 */
class com.troyworks.framework.tags.TestTagIndex extends Object {
	public function TestTagIndex() {
		super();
		//PASSED testNoMatch();
//PASSED
 	testColorMatch();
	//PASSED
	//  testShapeMatch();
	//PASSED
	//testShapeColorMatch();
	  
	}
	function getSquareTarget() : Object{
		var o : Object = new Object();
		o.matchesColors = ["Red1", "Red2"];
		o.matchesShapes = ["Square", "Rectangle"];
		return o;
	}
	function getBlueSquareClip0() : Object{
		var o : Object = new Object();
		o.matchesColors = ["Blue"];
		o.matchesShapes = ["Square", "Rectangle"];
		return o;
	}
	function getRedSquareClip1() : Object{
		var o : Object = new Object();
		o.matchesColors = ["Red1"];
		o.matchesShapes = ["Square", "Rectangle"];
		return o;
	}
	function getRedSquareClip2() : Object{
		var o : Object = new Object();
		o.matchesColors = ["Red2"];
		o.matchesShapes = ["Rectangle"];
		return o;
	}
	function getRedCircleClip3() : Object{
		var o : Object = new Object();
		o.matchesColors = ["Red2"];
		o.matchesShapes = ["Circle"];
		return o;
	}
	//---------------------------------------
	function getNoMatchGame() : Object{
		var o : Object = new Object();
		return o;
	}
	function getShapesMatchGame() : Object{
		var o : Object = new Object();
		o.matchesShapes = ["*"];
		return o;
	}
	function getColorMatchGame() : Object{
		var o : Object = new Object();
		o.matchesColors = ["*"];
		return o;
	}
	function getColorShapeMatchGame() : Object{
		var o : Object = new Object();
		o.matchesShapes = ["*"];
		o.matchesColors = ["*"];
		return o;
	}
	function getAllShapes() : Array{
		var mA : Array = new Array();
		mA.push(getBlueSquareClip0());
		mA.push(getRedSquareClip1());
		mA.push(getRedSquareClip2());
		mA.push(getRedCircleClip3());
		return mA;
	}
		 /*need to build a tag space
		  * that can look at the tag context, filter and index the 
		  * values
		  */
	public function testShapeColorMatch() : Void{
		trace("HIGHLIGHTP testShapeColorMatch...............");
		////////////  ADD GAME FILTERS /////////////////
		var G0 : Object = getColorShapeMatchGame();
		var tSpace : TagSpace = new TagSpace();
		tSpace.addToWhiteListContextsIn(G0);
		////////  ADD TARGETS /////////////////////
				trace("HIGHLIGHT2 ADD TARGETS----------------");
		var sTG : Object = getSquareTarget();
		tSpace.addToMatchingSpace(sTG);
		trace("HIGHLIGHT2 FLATTENING----------------");
		tSpace.flatten();
		////////   BUILD COLLISION SHADOWS /////////
		trace("HIGHLIGHT3 building collision shadows-----------------");
		var mAry : Array = getAllShapes();
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.buildCollisionShadow(mAry[i]);
	    }
	    ////////  CHECK Moving /////////////////////
	    trace("HIGHLIGHT4 checking collisions-----------------");
		for(var i : Number = 0; i < mAry.length; i++){
					trace("checking " + mAry[i].matchesShapes +  " " + mAry[i].matchesColors);
	
			tSpace.checkMatch(MaskedBitFlag(mAry[i].bf));
	    }
	}  
	public function testShapeMatch() : Void{
		trace("HIGHLIGHTP testShapeMatch...............");
		////////////  ADD GAME FILTERS /////////////////
		var G0 : Object = getShapesMatchGame();
		var tSpace : TagSpace = new TagSpace();
		tSpace.addToWhiteListContextsIn(G0);
		////////  ADD TARGETS /////////////////////
				trace("HIGHLIGHT2 ADD TARGETS----------------");
		var sTG : Object = getSquareTarget();
		tSpace.addToMatchingSpace(sTG);
		trace("HIGHLIGHT2 FLATTENING----------------");
		tSpace.flatten();
		////////   BUILD COLLISION SHADOWS /////////
		trace("HIGHLIGHT3 building collision shadows-----------------");
		var mAry : Array = getAllShapes();
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.buildCollisionShadow(mAry[i]);
	    }
	    ////////  CHECK Moving /////////////////////
	    trace("HIGHLIGHT4 checking collisions-----------------");
		for(var i : Number = 0; i < mAry.length; i++){
			trace("checking " + mAry[i].matchesShapes +  " " + mAry[i].matchesColors);
			tSpace.checkMatch(MaskedBitFlag(mAry[i].bf));
	    }
	}
	public function testColorMatch() : Void{
		trace("HIGHLIGHTP testColorMatch...............");
		////////////  ADD GAME FILTERS /////////////////
		var G0 : Object = getColorMatchGame();
		var tSpace : TagSpace = new TagSpace();
		tSpace.addToWhiteListContextsIn(G0);
		////////  ADD TARGETS /////////////////////
				trace("HIGHLIGHT2 ADD TARGETS----------------");
		var sTG : Object = getSquareTarget();
		tSpace.addToMatchingSpace(sTG);
		trace("HIGHLIGHT2 FLATTENING----------------");
		tSpace.flatten();
		////////   BUILD COLLISION SHADOWS /////////
		trace("HIGHLIGHT3 building collision shadows-----------------");
		var mAry : Array = getAllShapes();
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.buildCollisionShadow(mAry[i]);
	    }
	    ////////  CHECK Moving /////////////////////
	    trace("HIGHLIGHT4 checking collisions-----------------");
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.checkMatch(MaskedBitFlag(mAry[i].bf));
	    }
	}

	public function testNoMatch() : Void{
		trace("HIGHLIGHTP testNoMatch...............");
		////////////  ADD GAME FILTERS /////////////////
		var G0 : Object = getNoMatchGame();
		var tSpace : TagSpace = new TagSpace();
		tSpace.addToWhiteListContextsIn(G0);
		////////  ADD TARGETS /////////////////////
		var sTG : Object = getSquareTarget();
		tSpace.addToMatchingSpace(sTG);
		////////   BUILD COLLISION SHADOWS /////////
		var mAry : Array = getAllShapes();
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.buildCollisionShadow(mAry[i]);
	    }
	    ////////  CHECK Moving /////////////////////
		for(var i : Number = 0; i < mAry.length; i++){
			tSpace.checkMatch(mAry[i]);
	    }
	}

	public function testTagIndex() : Void{
		var ti : TagIndex = new TagIndex();

		var apple : Object = new Object();
		apple.tags = new Array();
		apple.tags.push("apple");
		apple.tags.push("fruit");
		apple.tags.push("green");

		var blueberry : Object = new Object();
		blueberry.tags = new Array();
		blueberry.tags.push("blueberry");
		blueberry.tags.push("fruit");
		blueberry.tags.push("blue");
		///////////////// INDEX TERMS /////////////////////
		trace("HIGHLIGHTp adding apple terms...............");
		ti.addTerms(apple.tags);
		trace("HIGHLIGHTp adding blueberry terms...............");
		ti.addTerms(blueberry.tags);
	//////////////////// ALL TERMS ADDED ///////////////////
		trace("HIGHLIGHTl adding apple terms...............");
		apple.bf = ti.createBitFlag(apple.tags);
		trace("HIGHLIGHTl adding blueberry terms...............");
		blueberry.bf = ti.createBitFlag(blueberry.tags);

		var fruitTagged : Object = ti.createBitFlag(["fruit"]);
		var blueTagged : Object = ti.createBitFlag(["blue"]);
		var greeblueTagged : Object = ti.createBitFlag(["blue", "green", "fruit"]);
		var isMatch : Boolean = BitFlag.isMatch(apple.bf.b,blueberry.bf.b, fruitTagged.b);
		if(isMatch){
			trace("HIGHLIGHTG  apple and blueberry are fruit");
		}else{
			trace("ERROR, apple and blueberry aren't fruit?!");
		}
		var isPMatch : Boolean = BitFlag.isPartialMatch(apple.bf.b,blueberry.bf.b);
		if(isMatch){
			trace("HIGHLIGHTG  apple and blueberry overlap");
		}else{
			trace("ERROR, apple and blueberry don't overlap?!");
		}
		var isBlueMatch1 : Boolean = BitFlag.isMatch(apple.bf.b,blueTagged.b, blueTagged.m);
		if(isBlueMatch1){
			trace("ERROR  apple's is blue?!");
		}else{
			trace("HIGHLIGHTG, apple is NOT blue");
		}
		var isBlueMatch2 : Boolean = BitFlag.isMatch(blueberry.bf.b,blueTagged.b, blueTagged.m);
		if(!isBlueMatch2){
			trace("ERROR  blueberry isn't blue?!");
		}else{
			trace("HIGHLIGHTG, blueberry IS blue");
		}
		////////////////
		var isBlueMatch3 : Number = BitFlag.isPartialMatchDetailed(blueberry.bf.b,blueTagged.b, blueTagged.m);
		var idx : Array = BitFlag.getOnBitBF(isBlueMatch3, blueTagged.m);
		var terms : Array = ti.getTermsByBF(idx);
		trace("found blue matches at positions " + idx + " which are " + terms);

		var isBlueMatch4 : Number = BitFlag.isPartialMatchDetailed(blueberry.bf.b,greeblueTagged.b, greeblueTagged.m);
		var idx2 : Array = BitFlag.getOnBitBF(isBlueMatch4, greeblueTagged.m);
		var terms2 : Array = ti.getTermsByBF(idx2);
		trace("found tag matches at positions " + idx2 + " which are " + terms2);
	}
	
}